M
Tidied things up a bit and added some constants to make it 'easier' to see what is going on, may get round to writing a module to for the MCP23017.
Any guidance on what the interfaces should look like any where would be useful..?
#!/usr/bin/python
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>
#
I2CBUS = 0x00 # I2C bus number (Always 0 on WiPy)
ADDRESS = 0x20 # I2C address of I/O expander.
IODIRA = 0x00 # Port A data direction register.
IODIRB = 0x01 # Port B data direction register.
IPOLA = 0x02
IPOLB = 0x03
GPINTENA = 0x04
GPINTENB = 0x05
DEFVALA = 0x06
DEFVALB = 0x07
INTCONA = 0x08
INTCONB = 0x09
IOCON = 0x0A
GPPUA = 0x0C
GPPUB = 0x0D
INTFA = 0x0E
INTFB = 0x0F
INTCAPA = 0x10
INTCAPB = 0x11
GPIOA = 0x12 # Port A data register.
GPIOB = 0x13 # Port B data register.
OLATA = 0x14
OLATB = 0x15
from machine import I2C
# Initialize I2C bus
i2c = I2C(I2CBUS, I2C.MASTER, baudrate=100000)
# Configure all pins on Port A and B as outputs.
i2c.writeto_mem (ADDRESS, IODIRA, bytes([0x00, 0x00]))
# Send 0xf0 to Port A data register (GPIOA)
i2c.writeto_mem (ADDRESS, GPIOA, bytes([0xf0]))
# Clear all outputs and reset data direction bits.
i2c.writeto_mem (ADDRESS, GPIOA, bytes([0x00,0x00]))
i2c.writeto_mem (ADDRESS, IODIRA, bytes([0x00, 0x00]))
I was going to post something slightly more useful, but had a hardware problem that has stopped me developing anything further for now (think it was just a broken jumper cable but I haven't had time to do any more yet).